package httpx

import (
	"encoding/json"
	"io"
	"net/http"
)

type Response struct {
	data    []byte
	Origin  *http.Response
	Success bool
}

func newResponse(response *http.Response) (*Response, error) {
	defer response.Body.Close()
	data, err := io.ReadAll(response.Body)
	if err != nil {
		return nil, err
	}
	return &Response{
		Origin:  response,
		data:    data,
		Success: response.StatusCode == http.StatusOK,
	}, nil
}

func (r *Response) Data() []byte {
	return r.data
}

func (r *Response) DataString() string {
	return string(r.data)
}

func (r *Response) Bind(o interface{}) error {
	return json.Unmarshal(r.data, o)
}
